package com.wyh.utils.sqlUtils.house;

import com.wyh.entity.Houses;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

public class addHouseSqlDynamic {
        public String addDynamic(@NotNull Houses houses) {
            StringBuilder builder = new StringBuilder();
            StringBuilder parameterPlaceholders = new StringBuilder();
            List<String> parameters = new ArrayList<>();
            if (houses.getHouse_id()>0) {
                if (builder.length() > 0) {
                    builder.append(", ");
                    parameterPlaceholders.append(", ");
                }
                builder.append("house_id ");
                parameterPlaceholders.append("#{house_id}");
                parameters.add("house_id");
            }
            if (houses.getAddress() != null) {
                if (builder.length() > 0) {
                    builder.append(", ");
                    parameterPlaceholders.append(", ");
                }
                builder.append("address ");
                parameterPlaceholders.append("#{address}");
                parameters.add("address");
            }
            if (houses.getState()!=null) {
                if (builder.length() > 0) {
                    builder.append(", ");
                    parameterPlaceholders.append(", ");
                }
                builder.append("state ");
                parameterPlaceholders.append("#{state}");
                parameters.add("state");
            }
            if (houses.getRemark()!=null){
                if (builder.length() > 0) {
                    builder.append(", ");
                    parameterPlaceholders.append(", ");
                }
                builder.append("remark ");
                parameterPlaceholders.append("#{remark}");
                parameters.add("remark");
            }

            StringBuilder sql = new StringBuilder();
            sql.append("INSERT INTO house (");
            sql.append(builder);
            sql.append(") VALUES (");
            sql.append(parameterPlaceholders);
            sql.append(");");

            System.out.println(sql);
            return sql.toString();

        }
}
